package com.line.config.datasource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplatePage {

	private Logger log = LoggerFactory.getLogger(JdbcTemplatePage.class);

	private int pageNo; // 当前页
	private int pageSize; // 一页显示的记录数，以及返回的一页记录数
	private int totalElements;// 总记录数
	private int totalPages; // 总页数

	private int startIndex; // 开始下标值
	private int lastIndex; // 最后下标值

	private int size;// 最终返回的当前页的记录数

	// 结果集存放List
	private List<Map<String, Object>> resultList;

	public JdbcTemplatePage(String sql, int pageNo, int pageSize, JdbcTemplate jdbcTemplate) {
		getPage(sql, pageNo, pageSize, jdbcTemplate, null);
	}

	public JdbcTemplatePage(String sql, int pageNo, int pageSize, JdbcTemplate jdbcTemplate, Object[] objects) {
		getPage(sql, pageNo, pageSize, jdbcTemplate, objects);

	}

	private void getPage(String sql, int pageNo, int pageSize, JdbcTemplate jdbcTemplate, Object[] objects) {
		if (jdbcTemplate == null) {
			throw new IllegalArgumentException("Page.jTemplate is null");
		} else if (sql == null || sql.equals("")) {
			throw new IllegalArgumentException("Page.sql is empty");
		}
		if (pageSize == 0) {
			pageSize = 10;
		}
		// 设置每页显示记录数
		setPageSize(pageSize);
		if (pageNo == 0) {
			pageNo = 1;
		}
		// 设置要显示的页数
		setPageNo(pageNo);
		// 计算总记录数
		StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
		totalSQL.append(sql);
		totalSQL.append(" ) totalTable ");
		log.info("分页查询，数量SQL:{}", totalSQL.toString());
		List<Map<String, Object>> list = null;
		if (objects == null) {
			list = jdbcTemplate.queryForList(totalSQL.toString());
		} else {
			list = jdbcTemplate.queryForList(totalSQL.toString(), objects);
		}
		Map<String, Object> map = list.get(0);
		Set<String> set = map.keySet();
		int total = 0;
		for (String count : set) {
			if (map.get(count) != null)
				total = Integer.parseInt(map.get(count).toString());
		}
		// 总记录数
		setTotalElements(total);
		// 计算总页数
		setTotalPages();
		// 计算起始行数
		setStartIndex();
		// 计算结束行数
		setLastIndex();
		// 计算当前页记录数
		setSize();
		// 使用mysql时直接使用limits
		StringBuffer paginationSQL = new StringBuffer();
		paginationSQL.append(sql);
		paginationSQL.append(" limit " + startIndex + "," + lastIndex);
		String sqlResult = paginationSQL.toString();
		log.info("分页查询，主体SQL：{}", sqlResult);
		// 装入结果集
		List<Map<String, Object>> listRes = null;
		if (objects == null) {
			listRes = jdbcTemplate.queryForList(sqlResult);
		} else {
			listRes = jdbcTemplate.queryForList(sqlResult, objects);
		}
		if (listRes == null) {
			listRes = new ArrayList<>();
		}
		setResultList(listRes);
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<Map<String, Object>> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map<String, Object>> resultList) {
		this.resultList = resultList;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages() {
		if (totalElements % pageSize == 0) {
			this.totalPages = totalElements / pageSize;
		} else {
			this.totalPages = (totalElements / pageSize) + 1;
		}
	}

	public Long getTotalElements() {
		return (long) totalElements;
	}

	public void setTotalElements(int totalElements) {
		this.totalElements = totalElements;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex() {
		this.startIndex = (pageNo - 1) * pageSize;
	}

	public int getLastIndex() {
		return lastIndex;
	}

	// 计算结束时候的索引
	public void setLastIndex() {
		if (totalElements < pageSize) {
			this.lastIndex = totalElements;
		} else if ((totalElements % pageSize == 0) || (totalElements % pageSize != 0 && pageNo < totalPages)) {
			this.lastIndex = pageNo * pageSize;
		} else if (totalElements % pageSize != 0 && pageNo == totalPages) {// 最后一页
			this.lastIndex = totalElements;
		}
	}

	public int getSize() {
		return size;
	}

	public void setSize() {
		if (pageNo < totalPages) {
			this.size = pageSize;
		} else if (pageNo == totalPages) {
			if (totalElements != 0) {
				this.size = totalElements % pageNo;
			} else {
				this.size = totalElements;
			}

		}
	}
}
